import * as turf from '@turf/turf'
import mapboxgl from "mapbox-gl";
import {GET_MAPBOX_MAP} from "./CreateMap";
import uavIcon from "../../assets/img/uavIcon.jpeg"

let animatePointGeoJson = null;

// 无人机实时飞行
const MAP_UAV_FLIGHT_ING = (values, layerKey = '无人机飞行') => {

    // ClearUAVMap();

    const map = GET_MAPBOX_MAP();

    map.loadImage(uavIcon, function (error, img) {
        if (error) throw error;

        map.addImage('uavIcon', img);
    });

    const allPoints = [];

    allPoints.push(
        turf.point([values.droneLongitude, values.droneLatitude], {
            ...values,
            layerKey,
            bearing: values.yaw,
        }),
    );

    // 特征集合
    animatePointGeoJson = turf.featureCollection(allPoints);

    map.addLayer({
        id: 'animatePointLayer',
        type: 'symbol',
        source: {
            type: 'geojson',
            data: animatePointGeoJson,
        },
        layout: {
            'icon-image': 'uavIcon',
            'icon-size': 0.5,
            'icon-rotate': ['get', 'bearing'],
            'icon-rotation-alignment': 'map',
            'icon-allow-overlap': true,
            'icon-ignore-placement': true,
        },
    });
    // counter = counter + 1;
};

// 清除轨迹
// const ClearUAVMap = () => {
//     if (map.hasImage('uavIcon')) map.removeImage('uavIcon');
//
//     if (map.getLayer('animatePointLayer')) map.removeLayer('animatePointLayer');
//     if (map.getSource('animatePointLayer')) map.removeSource('animatePointLayer');
//
//     if (map.getLayer('homePointLayer')) map.removeLayer('homePointLayer');
//     if (map.getSource('homePointLayer')) map.removeSource('homePointLayer');
// };

export default MAP_UAV_FLIGHT_ING;
